(function ($) {

AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({

  beforeRequest: function () {
    $('#loader').css("display","block");
    //goto top
    window.scrollTo(0,0);
  },

  facetLinks: function (facet_field, facet_values) {
    var links = [];
    if (facet_values) {
      for (var i = 0, l = facet_values.length; i < l; i++) {
        links.push(AjaxSolr.theme('facet_link', facet_values[i], this.facetHandler(facet_field, facet_values[i])));
      }
    }
    return links;
  },

  facetHandler: function (facet_field, facet_value) {
    var self = this;
    return function () {
      self.manager.store.remove('fq');
      self.manager.store.addByValue('fq', facet_field + ':' + facet_value);
      self.manager.doRequest(0);
      return false;
    };
  },
  afterRequest: function () {
    $('#loader').css("display","none");
    $(this.target).empty();
    for (var i = 0, l = this.manager.response.response.docs.length; i < l; i++) {
      var doc = this.manager.response.response.docs[i];
      $(this.target).append(AjaxSolr.theme('result', doc, AjaxSolr.theme('snippet', doc)));


      //var items = [];
      //items = items.concat(this.facetLinks('id', doc.id));
      //to show tag links
      //AjaxSolr.theme('list_items', '#links_' + doc.id, items);
    }
    if(this.manager.response.response.numFound == 0) {
        var x = this.manager.response.responseHeader.params.q;
        $(this.target).html("No results for query");
    }
  },

  init: function () {
   /*
    $('a.more').livequery(function () {
      $(this).toggle(function () {
        $(this).parent().find('span').show();
        $(this).text('less');
        return false;
      }, function () {
        $(this).parent().find('span').hide();
        $(this).text('more');
        return false;
      });
    });
    */
  }

});

})(jQuery);
